<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel">
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type: checkval}"
        class="j-table-force-nowrap"
        @change="handleTableChange"
        :customRow='rowProperties'
        :rowClassName="rowClassName"
      >

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

  </a-card>
  </j-modal>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { filterDictTextByCache, filterMultiDictText, initDictOptions } from '@/components/dict/JDictSelectUtil'

  export default {
    name: 'BusinessTypeList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
    },
    props:{
      checkval:{
        type: String,
        required: false,
        default: 'radio'
      },
      businessNode:{
        type: Number,
        required: true
      },
      businessType: {
        type: Number,
        required: false
      }
    },
    data () {
      return {
        title:'业务类型',
        disableMixinCreated: true,
        width:800,
        visible: false,
        disableSubmit: false,
        description: '业务类型管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'业务节点',
            align:"center",
            dataIndex: 'businessNode',
            customRender: (text) => {
              //字典值替换通用方法
              return filterDictTextByCache('business_node_type', text);
            }
          },
          {
            title:'物料种类',
            align:"center",
            dataIndex: 'materialTypeName'
          },
          {
            title:'业务类型',
            align:"center",
            dataIndex: 'businessType',
            customRender: (text) => {
              //字典值替换通用方法
              return filterDictTextByCache('business_type', text);
            }
          },
          {
            title:'对库存影响',
            align:"center",
            dataIndex: 'inventoryEffect'
          },
          {
            title:'关联单据',
            align:"center",
            dataIndex: 'relationalReceipts'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark'
          },
          // {
          //   title: '操作',
          //   dataIndex: 'action',
          //   align:"center",
          //   fixed:"right",
          //   width:147,
          //   scopedSlots: { customRender: 'action' }
          // }
        ],
        url: {
          list: "/business/businessType/list",
          delete: "/business/businessType/delete",
          deleteBatch: "/business/businessType/deleteBatch",
          exportXlsUrl: "/business/businessType/exportXls",
          importExcelUrl: "business/businessType/importExcel",
        },
        dictOptions:{},
        superFieldList:[],
        selectedRowKey: null,
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      rowProperties(record, index) {
        return {
          on:{
            click: (event)=>{
              this.selectedRowKeys = [record.id]
              this.selectedRowKey = record.id
              this.$nextTick(() => {
                const rows = this.$refs.table.$el.querySelectorAll('tr'); // 获取所有的 tr 元素
                rows.forEach(row => {
                  // 动态删除 tr 元素的类名
                  row.classList.remove('ant-table-row-selected');
                });
              });
            }
          }
          // onClick: (event) => {
          //   alert(888888)
          // },
        }
      },
      rowClassName(record) {
        return this.selectedRowKey === record.id ? 'selected-row' : '';
      },
      onSelectChange(selectedRowKeys, selectionRows) {
        this.selectedRowKeys = selectedRowKeys;
        this.selectionRows = selectionRows;
        this.selectedRowKey = this.selectedRowKeys[0]
      },
      add() {
        this.showModal();
      },
      showModal() {
        this.visible = true;
        this.queryParam = { businessNode: this.businessNode ,businessType: this.businessType}
        this.loadData();
      },
      edit (record) {
        this.visible=true
        this.$nextTick(()=>{
          // this.$refs.realForm.edit(record);
        })
      },
      close() {
        // this.searchReset(false);
        this.queryParam = {}
        this.selectedRowKeys = []
        this.selectionRows = []
        this.visible = false;
      },
      handleOk () {
        if(this.selectionRows.length==0){
          this.$message.warn('请选择业务类型！')
          return;
        }
        this.$emit('handleOk', {
            rows: this.selectionRows,
            callback: (success) => {
              if(success){
                this.close()
              }
            },
          }
        )
      },
      handleCancel () {
        this.selectedRowKey = ''
        this.close()
      },
      initDictConfig(){
        //初始化字典 - 开关
        // initDictOptions('yes_no').then((res) => {
        //   console.log(res);
        //   if (res.success) {
        //     console.log(res.result);
        //     this.openDictOptions = res.result;
        //   }
        // });
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'businessNode',text:'业务节点',dictCode:'business_node_type'})
        fieldList.push({type:'string',value:'materialTypeId',text:'物料种类id',dictCode:''})
        fieldList.push({type:'string',value:'materialTypeName',text:'物料种类',dictCode:''})
        fieldList.push({type:'string',value:'businessType',text:'物料类型',dictCode:'business_type'})
        fieldList.push({type:'string',value:'inventoryEffect',text:'对库存影响',dictCode:''})
        fieldList.push({type:'string',value:'relationalReceipts',text:'关联单据',dictCode:''})
        fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
        this.superFieldList = fieldList
      },
      //编辑字典数据
      editItem(record) {
        // this.$refs.goodsSupplierList.edit(record);
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';

</style>
<style >
 .selected-row {
  background-color: orange  !important; /* Customize the color */
}
 /** 设置鼠标划过为透明色，否则导致选中的颜色呗覆盖  **/
 .ant-table-tbody > tr:hover:not(.ant-table-expanded-row) > td,.ant-table-row-hover,.ant-table-row-hover>td{
   background-color: transparent !important;
 }

 .ant-table-fixed .ant-table-row-hover,.ant-table-row-hover>td{
   background-color: transparent !important;
 }

</style>
